package cn.gudian.cms.Admin
class AdminRoleController {

  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [adminRoleInstanceList: AdminRole.list(params), adminRoleInstanceTotal: AdminRole.count()]
  }

  def create = {
    def adminRoleInstance = new AdminRole()
    adminRoleInstance.properties = params
    return [adminRoleInstance: adminRoleInstance]
  }

  def save = {
    def adminRoleInstance = new AdminRole(params)
    def roles = request.getParameterValues("roles")
    if (roles == null) {
      flash.message = "权限不能为空"
      render(view: "create", model: [adminRoleInstance: adminRoleInstance])
      return
    }
    def rolesString = ""
    for (int i = 0; i < roles.length; i++) {
      rolesString = rolesString + roles[i] + ","
    }
    adminRoleInstance.roles = rolesString
    if (!adminRoleInstance.hasErrors() && adminRoleInstance.save()) {
      flash.message = "adminRole.created"
      flash.args = [adminRoleInstance.id]
      flash.defaultMessage = "AdminRole ${adminRoleInstance.id} created"
      redirect(action: "show", id: adminRoleInstance.id)
    }
    else {
      render(view: "create", model: [adminRoleInstance: adminRoleInstance])
    }
  }

  def show = {
    def adminRoleInstance = AdminRole.get(params.id)
    if (!adminRoleInstance) {
      flash.message = "adminRole.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "AdminRole not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [adminRoleInstance: adminRoleInstance]
    }
  }

  def edit = {
    def adminRoleInstance = AdminRole.get(params.id)
    if (!adminRoleInstance) {
      flash.message = "adminRole.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "AdminRole not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [adminRoleInstance: adminRoleInstance]
    }
  }

  def update = {
    def adminRoleInstance = AdminRole.get(params.id)
    if (adminRoleInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (adminRoleInstance.version > version) {

          adminRoleInstance.errors.rejectValue("version", "adminRole.optimistic.locking.failure", "Another user has updated this AdminRole while you were editing")
          render(view: "edit", model: [adminRoleInstance: adminRoleInstance])
          return
        }
      }

      def roles = request.getParameterValues("roles")
      if (roles == null) {
        flash.message = "权限不能为空"
        render(view: "edit", model: [adminRoleInstance: adminRoleInstance])
        return
      }
      def rolesString = ""
      for (int i = 0; i < roles.length; i++) {
        rolesString = rolesString + roles[i] + ","
      }
      adminRoleInstance.roles = rolesString
      adminRoleInstance.properties = params
      if (!adminRoleInstance.hasErrors() && adminRoleInstance.save()) {
        flash.message = "adminRole.updated"
        flash.args = [params.id]
        flash.defaultMessage = "AdminRole ${params.id} updated"
        redirect(action: "show", id: adminRoleInstance.id)
      }
      else {
        render(view: "edit", model: [adminRoleInstance: adminRoleInstance])
      }
    }
    else {
      flash.message = "adminRole.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "AdminRole not found with id ${params.id}"
      redirect(action: "edit", id: params.id)
    }
  }

  def delete = {
    def adminRoleInstance = AdminRole.get(params.id)
    if (adminRoleInstance) {
      try {
        adminRoleInstance.delete()
        flash.message = "adminRole.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "AdminRole ${params.id} deleted"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "adminRole.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "AdminRole ${params.id} could not be deleted"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "adminRole.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "AdminRole not found with id ${params.id}"
      redirect(action: "list")
    }
  }

  def deleteAll = {
    def qstr = params.qstr
    if (qstr) {
      try {
        AdminRole.executeUpdate("delete AdminRole where id in(" + qstr + ")")
        flash.message = "delete successfully:" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "delete failed:" + qstr
        redirect(action: "list")
      }
    }

  }
}
